﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#endregion

namespace TheSpirit
{
    public static class InputManager
    {
        #region Keyboard Date

        private static KeyboardState previousKeyboardState;
        private static KeyboardState currentKeyboardState;


        public static KeyboardState CurrentKeyboardState
        {
            get { return currentKeyboardState; }
        }

        public static bool IsKeyPressed(Keys key)
        {
            return currentKeyboardState.IsKeyDown(key);
        }

        public static bool IsKeyTriggered(Keys key)
        {
            return (currentKeyboardState.IsKeyDown(key)) &&
                (!previousKeyboardState.IsKeyDown(key));
        }

        #endregion


        #region Mouse Data

        private static Texture2D[] mouseTexture = new Texture2D[10];
        private static MouseState previousMouseState;
        private static MouseState currentMouseState;
        public enum MouseCursor
        {
            Normal,
            Attack,
            Select
        }
        public static MouseCursor mouseCursor = MouseCursor.Normal;
        
        
        public static MouseState CurrentMouseState
        {
            get { return currentMouseState; }
        }
        public static Rectangle MousePosition
        {
            get { return new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 20, 20); }
        }
        
        public static bool IsLeftClicked()
        {
            if (currentMouseState.LeftButton == ButtonState.Released &&
                previousMouseState.LeftButton == ButtonState.Pressed)
                return true;
            return false;
        }
        public static bool IsRightClicked()
        {
            if (currentMouseState.RightButton == ButtonState.Released &&
                previousMouseState.RightButton == ButtonState.Pressed)
                return true;
            return false;
        }
        public static bool IsLeftClicking()
        {
            if (currentMouseState.LeftButton == ButtonState.Pressed &&
                previousMouseState.LeftButton == ButtonState.Pressed)
                return true;
            return false;
        }
        public static bool IsRightClicking()
        {
            if (currentMouseState.RightButton == ButtonState.Pressed &&
                previousMouseState.RightButton == ButtonState.Pressed)
                return true;
            return false;
        }

        private static bool rightEnd = false;
        private static bool leftEnd = false;
        private static bool upEnd = false;
        private static bool downEnd = false;

        public static bool RightEnd
        {
            get { return rightEnd; }
        }
        public static bool LeftEnd
        {
            get { return leftEnd; }
        }
        public static bool UpEnd
        {
            get { return upEnd; }
        }
        public static bool DownEnd
        {
            get { return downEnd; }
        }

        #endregion


        #region Update

        public static void LoadContent(ContentManager content)
        {
            mouseTexture[0] = content.Load<Texture2D>("Mouse/Normal");
            mouseTexture[1] = content.Load<Texture2D>("Mouse/Attack");
            mouseTexture[2] = content.Load<Texture2D>("Mouse/Select");
        }

        public static void Update()
        {
            rightEnd = false;
            leftEnd = false;
            upEnd = false;
            downEnd = false;

            previousKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();

            previousMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();

            if (Mouse.GetState().X >= (Constants.WIDTH - MousePosition.Width - 1))
            {
                rightEnd = true;
                Mouse.SetPosition((Constants.WIDTH - MousePosition.Width-1), Mouse.GetState().Y);
            }
            if (Mouse.GetState().X <= 1)
            {
                leftEnd = true;
                Mouse.SetPosition(1, Mouse.GetState().Y);
            }
            if (Mouse.GetState().Y >= (Constants.HEIGHT - MousePosition.Height - 1))
            {
                downEnd = true;
                Mouse.SetPosition(Mouse.GetState().X, (Constants.HEIGHT - MousePosition.Height - 1));
            }
            if (Mouse.GetState().Y <= 1)
            {
                upEnd = true;
                //Mouse.SetPosition(Mouse.GetState().X, 1);
            }
        }

        public static void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();

            spriteBatch.Draw(mouseTexture[(int)mouseCursor],
                new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 
                    mouseTexture[(int)mouseCursor].Width, 
                    mouseTexture[(int)mouseCursor].Height),
                Color.White);

            spriteBatch.End();
        }

        public static void ChangeMouseCursor(string name)
        {
            switch(name)
            {
                case "Normal":
                    mouseCursor = MouseCursor.Normal;
                    break;
                case "Attack":
                    mouseCursor = MouseCursor.Attack;
                    break;
                case "Select":
                    mouseCursor = MouseCursor.Select;
                    break;
                default:
                    break;
            }
        }

        public static Texture2D MouseTexture()
        {
            switch (mouseCursor)
            {
                case MouseCursor.Normal:
                    return mouseTexture[0];
                case MouseCursor.Attack:
                    return mouseTexture[1];
                case MouseCursor.Select:
                    return mouseTexture[2];
                default:
                    break;
            }
            return mouseTexture[0];
        }

        #endregion
    }
}
